package com.note.simpleshop.module.user.controller;
import com.note.simpleshop.constant.ErrorCode;
import com.note.simpleshop.entity.TbUser;
import com.note.simpleshop.module.user.auth.UserUtils;
import com.note.simpleshop.module.user.exception.authc.AccountException;
import com.note.simpleshop.module.user.exception.authc.PasswordException;
import com.note.simpleshop.module.user.service.UserService;
import com.note.simpleshop.vo.Msg;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("user")
public class LoginController {
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private UserService userService;
    @ApiOperation(value = "getUserInfo", notes = "获取用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header")})
    @PostMapping(value = "getUserInfo")
    public Msg getUserInfo()
    {
        TbUser user= UserUtils.currentUsers();
        Msg msg = new Msg();
        msg.setErrorCode(ErrorCode.ERROR_0000);
        msg.setData(user);
        return msg;
    }
    @ApiOperation(value = "login", notes = "用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query")
    })
    @PostMapping(value = "get")
    public Msg getToken(@RequestParam String mobile, @RequestParam String password) {
        logger.info("**********mobile={},password={}",mobile,password);
        Msg msg = new Msg();
        try
        {
            if (StringUtils.isBlank(mobile)) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("手机号不能为空");
                return msg;
            }
            if (StringUtils.isBlank(password)) {
                msg.setErrorCode(ErrorCode.ERROR_1000000);
                msg.setMsg("密码不能为空");
                return msg;
            }
            String token=userService.login(mobile,password);
            msg.setData(token);
            msg.setErrorCode(ErrorCode.ERROR_0000);
            msg.setMsg("ok");
        }
        catch(AccountException e)
        {
            msg.setErrorCode(ErrorCode.ERROR_1000000);
            msg.setMsg(e.getMessage());
        }
        catch(PasswordException e)
        {
            msg.setErrorCode(ErrorCode.ERROR_1000000);
            msg.setMsg(e.getMessage());
        }
        catch (Exception e)
        {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000001);
            msg.setMsg(e.getMessage());
        }
        return msg;
    }
}
